package com.msb.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.mall.product.dao.CategoryDao;
import com.msb.mall.product.entity.CategoryBrandRelationEntity;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.CategoryBrandRelationService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> queryCategoryByTree() {
        List<CategoryEntity> list = baseMapper.selectList(null);
        List<CategoryEntity> list2 = baseMapper.selectList(null).stream().filter(entity -> entity.getParentCid()==0)
                .map(entity -> {
                    entity.setChildrens(getCategroyChildrens(entity, list));
                    return entity;
                }).sorted(Comparator.comparingInt(entity -> (entity.getSort() == null ? 0 : entity.getSort()))).collect(Collectors.toList());
        return list2;
    }

    @Transactional
    @Override
    public void updateDetails(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", category.getCatId())).forEach(item->{
            item.setCatelogName(category.getName());
            categoryBrandRelationService.updateById(item);
        });
    }

    @Override
    public Long[] findCategoryPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    /**
     * 225,22,2
     * @param catelogId
     * @param paths
     * @return
     */
    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity entity = this.getById(catelogId);
        if(entity.getParentCid() != 0){
            findParentPath(entity.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getCategroyChildrens(CategoryEntity entity, List<CategoryEntity> list) {
        List<CategoryEntity> collect = list.stream().filter(categoryEntity -> {
            return Objects.equals(categoryEntity.getParentCid(), entity.getCatId());
        }).map(category -> {
            category.setChildrens(getCategroyChildrens(category, list));
            return category;
        }).sorted(Comparator.comparingInt(entity2 -> (entity2.getSort() == null ? 0 : entity2.getSort()))).collect(Collectors.toList());
        return collect;
    }

}